package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.impl.dao.GovernmentUserDutyDao;
import com.jiantong.user.model.GovernmentUserDuty;
import com.jiantong.user.service.GovernmentUserDutyReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/6.
 *
 */
@Slf4j
@Service
@RpcProvider
public class GovernmentUserDutyReadServiceImpl implements GovernmentUserDutyReadService{

    private final GovernmentUserDutyDao governmentUserDutyDao;

    @Autowired
    public GovernmentUserDutyReadServiceImpl(GovernmentUserDutyDao governmentUserDutyDao) {
        this.governmentUserDutyDao = governmentUserDutyDao;
    }

    @Override
    public Response<List<GovernmentUserDuty>> findGovernmentUserDutiesByUserId(Long userId,Integer userAppRole) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            if(userId != null){
                criteria.put("userId",userId);
            }
            if(userAppRole != null){
                criteria.put("userAppRole",userAppRole);
            }
            List<GovernmentUserDuty> dutyList = governmentUserDutyDao.findGovernmentUserDutiesByUserId(criteria);
            return Response.ok(dutyList);
        }catch (Exception e){
            log.error("failed to find dutyList(id={}), cause:{}", userId, Throwables.getStackTraceAsString(e));
            return Response.fail("dutyList.find.fail");
        }
    }
}
